package cn.kimoon.dau_android.Dialog;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import cn.kimoon.dau_android.R;

/**
 * Created by wei on 2016/11/14.
 */

public class DialogManager {

    private static DialogManager instance;
    //正在提交对话框
    private LoadingDialog loadingDialog;

    private DialogManager() {
    }

    /**
     * 获取单例
     *
     * @return DialogManager实例
     */
    public static DialogManager getInstance() {
        if (instance == null) {
            instance = new DialogManager();
        }
        return instance;
    }

	public void showToastDialog(Context context, LoadingDialog.LoadingState state, String title) {
		 if (context == null || !(context instanceof AppCompatActivity)) {
            return;
        }
        final AppCompatActivity activity = (AppCompatActivity) context;
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog();
        }
//		loadingDialog.setState(state);
        if (title != null && !TextUtils.isEmpty(title)) {
            //设置标题
//            loadingDialog.setTitle(title);
            loadingDialog.setState(state, title);
        } else {
            //默认标题
//            loadingDialog.setTitle("");
            loadingDialog.setState(state, "");
        }
		 //显示对话框
        if (!activity.isFinishing() && !loadingDialog.isAdded() && !loadingDialog.isVisible() && !loadingDialog.isRemoving()) {
            activity.getSupportFragmentManager().beginTransaction()
                    .add(loadingDialog, loadingDialog.getClass().getSimpleName())
                    .commitAllowingStateLoss();
                        activity.getSupportFragmentManager().executePendingTransactions();
        }
	}
   
    /**
     * 显示正在加载对话框
     *
     * @param context AppCompatActivity上下文
     * @param title   标题
     */
    public void showLoadingDialog(Context context, String title) {
        if (context == null || !(context instanceof AppCompatActivity)) {
            return;
        }
        final AppCompatActivity activity = (AppCompatActivity) context;
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog();
        }
//		loadingDialog.setState(LoadingDialog.LoadingState.STATE_START_LOADING);
        if (title != null && !TextUtils.isEmpty(title)) {
            //设置标题
//            loadingDialog.setTitle(title);
            loadingDialog.setState(LoadingDialog.LoadingState.STATE_START_LOADING, title);
        } else {
            //默认标题
//            loadingDialog.setTitle(context.getResources().getString(R.string.dialog_loading));
            loadingDialog.setState(LoadingDialog.LoadingState.STATE_START_LOADING, context.getResources().getString(R.string.dialog_loading));
        }
        //显示对话框
        if (!activity.isFinishing() && !loadingDialog.isAdded() && !loadingDialog.isVisible() && !loadingDialog.isRemoving()) {
            activity.getSupportFragmentManager().beginTransaction()
                    .add(loadingDialog, loadingDialog.getClass().getSimpleName())
                    .commitAllowingStateLoss();
            activity.getSupportFragmentManager().executePendingTransactions();
        }
    }

    /**
     * 隐藏正在加载对话框
     */
    public void dismissLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismissAllowingStateLoss();
        }
    }
}
